package com.leve.control.controller.app;

import com.leve.control.constant.Constants;
import com.leve.control.constant.ErrorCode;
import com.leve.control.domain.CuserDevice;
import com.leve.control.domain.Device;
import com.leve.control.service.DeviceService;
import com.leve.control.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author whongyu
 * @create by 2019/6/13
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/app/device")
@Api(tags = {"app端设备相关接口服务"})
public class AppDeviceController {

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "用户设备列表")
    @GetMapping(value = "/find")
    @ResponseBody
    public Result findDevice(@RequestParam String userId,@RequestParam int permission){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/app/device/find|用户设备列表,userId={},permission={}: "+ userId,permission);
        try{
            List<Device> devices = deviceService.findDevice(userId,permission);
            return new Result(ErrorCode.SUCCESS,devices);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/app/device/find|用户设备列表", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "查看设备信息接口")
    @GetMapping(value = "/query")
    @ResponseBody
    public Result queryDevice(@RequestParam int Id){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/app/device/query|查看设备信息接口,deviceId={}: "+ Id);
        try{
            Map<String, Object> map = deviceService.queryDevice(Id);
            return new Result(ErrorCode.SUCCESS,map);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/app/device/query|查看设备信息接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "设备授权接口（1:操作者 2:观察者）")
    @PostMapping(value = "/impower")
    @ResponseBody
    public Result deviceImpower(@RequestBody CuserDevice cuserDevice){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/app/device/impower|设备授权接口,cuserDevice={}: "+ cuserDevice);
        try{
            int i = deviceService.deviceImpower(cuserDevice);
            if(i>0){
                return new Result(ErrorCode.E_20013);
            }
            return new Result(ErrorCode.E_20029);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/app/device/impower|设备授权接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "设备信息中 删除接口")
    @DeleteMapping(value = "/remove")
    @ResponseBody
    public Result removeDeviceRele(@RequestParam int id){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/app/device/remove|设备信息中 删除接口,id={}: "+ id);
        try{
            int i = deviceService.removeDeviceRele(id);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }
            return new Result(ErrorCode.E_20029);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/app/device/remove|设备信息中 删除接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "设备用户添加接口")
    @PutMapping(value = "/add")
    @ResponseBody
    public Result addDeviceCuser(@RequestBody CuserDevice cuserDevice){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/app/device/add|设备用户添加接口,cuserDevice={}: "+ cuserDevice);
        try{
            int i = deviceService.addDeviceCuser(cuserDevice);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }
            return new Result(ErrorCode.E_20029);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/app/device/add|设备用户添加接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }




}
